package com.dalei.vdserver.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.dalei.vdserver.model.VehicleInformation;

public class VehicleInformationDaoImpl extends JdbcDaoSupport implements
		VehicleInformationDao {

	@Override
	public VehicleInformation getVehicleInformation(int id) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM vds_vehicle_information WHERE _id = ?";
		VehicleInformation vehicleInformation = null;
		try {
			vehicleInformation = (VehicleInformation) getJdbcTemplate()
					.queryForObject(sql, new Object[] { id }, new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							VehicleInformation vi = new VehicleInformation();
							vi.setId(rs.getInt("_id"));
							vi.setLicensePlateNumber(rs
									.getString("_licensePlateNumber"));
							vi.setLicensePlateType(rs
									.getInt("_licensePlateType"));
							vi.setVin(rs.getString("_vin"));
							vi.setVehicleType(rs.getInt("_vehicleType"));
							vi.setBrandAndModel(rs.getString("_brandAndmodel"));
							vi.setBodyColor(rs.getInt("_bodyColor"));
							vi.setEngineModel(rs.getString("_engineModel"));
							vi.setFuelType(rs.getInt("_fuelType"));
							vi.setOwners(rs.getString("_owners"));
							Timestamp timestamp = rs
									.getTimestamp("_registrationDate");
							Date date = new Date(timestamp.getTime());
							vi.setRegistrationDate(date);
							return vi;
						}
					});
		} catch (IncorrectResultSizeDataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return vehicleInformation;
	}

	@Override
	public VehicleInformation getVehicleInformationOfTask(int fid) {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_vehicle_information "
				+ "WHERE _id = "
				+ "(SELECT _vehicleInformationId FROM vds_tasks WHERE _fid = ?)";
		VehicleInformation vehicleInformation = null;
		try {
			vehicleInformation = (VehicleInformation) getJdbcTemplate()
					.queryForObject(sql, new Object[] { fid }, new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							VehicleInformation vi = new VehicleInformation();
							vi.setId(rs.getInt("_id"));
							vi.setLicensePlateNumber(rs
									.getString("_licensePlateNumber"));
							vi.setLicensePlateType(rs
									.getInt("_licensePlateType"));
							vi.setVin(rs.getString("_vin"));
							vi.setVehicleType(rs.getInt("_vehicleType"));
							vi.setBrandAndModel(rs.getString("_brandAndmodel"));
							vi.setBodyColor(rs.getInt("_bodyColor"));
							vi.setEngineModel(rs.getString("_engineModel"));
							vi.setFuelType(rs.getInt("_fuelType"));
							vi.setOwners(rs.getString("_owners"));
							Timestamp timestamp = rs
									.getTimestamp("_registrationDate");
							Date date = new Date(timestamp.getTime());
							vi.setRegistrationDate(date);
							return vi;
						}
					});
		} catch (IncorrectResultSizeDataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return vehicleInformation;
	}
}
